//
//  HXDNewfeatureViewController.swift
//  weibo
//
//  Created by huxuedong on 15/12/8.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

class HXDNewfeatureViewController: UIViewController, UIScrollViewDelegate {
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        return scrollView
    }()
    
    private lazy var pageControll: UIPageControl = {
        let pageControll = UIPageControl()
        pageControll.currentPageIndicatorTintColor = UIColor.greenColor()
        pageControll.pageIndicatorTintColor = UIColor.blueColor()
        return pageControll
    }()
    
    private lazy var enterButton: UIButton = {
        let enterButton = UIButton()
        enterButton.addTarget(self, action: "enterButtonClick", forControlEvents: .TouchUpInside)
        enterButton.setTitle("进入微博", forState: .Normal)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        enterButton.sizeToFit()
        return enterButton
    }()
    
    private lazy var shareButton: UIButton = {
        let shareButton = UIButton()
        shareButton.addTarget(self, action: "shareButtonClick:", forControlEvents: .TouchUpInside)
        shareButton.setTitle("分享到微博", forState: .Normal)
        shareButton.titleLabel?.font = UIFont.systemFontOfSize(13)
        shareButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        shareButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
        shareButton.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        shareButton.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        shareButton.sizeToFit()
        return shareButton
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI() {
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        pageControll.frame = CGRect(x: scrollView.frame.width * 0.5, y: scrollView.frame.height - 100, width: 0, height: 0)
        view.addSubview(pageControll)
        let count = 4
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(imageView)
            if i == (count - 1) {
                setLastPageView(imageView)
            }
        }
        scrollView.contentSize = CGSize(width: scrollView.frame.width * CGFloat(count), height: 0)
        pageControll.numberOfPages = count
    }
    
    private func setLastPageView(lastPageView: UIView) {
        lastPageView.userInteractionEnabled = true
        enterButton.center = CGPoint(x: lastPageView.frame.width * 0.5, y: lastPageView.frame.height - 150)
        shareButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
        lastPageView.addSubview(enterButton)
        lastPageView.addSubview(shareButton)
    }
    
    @objc private func enterButtonClick() {
        NSNotificationCenter.defaultCenter().postNotificationName("switchVC", object: nil)
    }
    
    @objc private func shareButtonClick(button: UIButton) {
        button.selected = !button.selected
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.width + 0.5
        pageControll.currentPage = Int(page)
    }

}
